var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "id",
			name : "id",
			hide : true
		}, {
			colkey : "bom_id",
			name : "物料编码",
		}, {
			colkey : "bom_name",
			name : "物料名称",
		}, {
			colkey : "in_number",
			name : "入库数量"
		}, {
			colkey : "out_number",
			name : "出库数量"
		}, {
			colkey : "before_number",
			name : "之前数量"
		}, {
			colkey : "current_number",
			name : "当前数量"
		}, {
			colkey : "storage_num",
			name : "所属位置	"
		}, {
			colkey : "type",
			name : "操作方式	",
			renderData : function(rowindex,data, rowdata, column) {
				if(data == 0){
					return "物料入库";
				}else if(data == 1){
					return "物料出库";
				}else if(data == 2){
					return "修改";
				}
			}
		}, {
			colkey : "create_date",
			name : "出入库时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd hh:mm:ss");
			}
		}
		],
		jsonUrl : rootPath + '/pda/getParameter.shtml',
		dymCol:true,
		checkbox : true,
		checkValue: "id",
		records: "list",
		totalPages: "pages",
		totalRecords: "total",
		pagecode: '10',
		pageNow : 'pageNum'
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var sel = $("#billstateid option:selected").val();//获取下拉框数据
		var myv = $("#btime").val();//获取输入框数据
		//放出到input框中
		$("#myValue").val(sel+","+myv);
		var searchParams = $("#myValue").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addAccount").click("click", function() {
		addAccount();
	});
	$("#editAccount").click("click", function() {
		editAccount();
	});
	$("#delAccount").click("click", function() {
		delAccount();
	});
	$("#view").click("click", function() {
		view();
	});

	$("#search").click("click", function() {
		var searchParams = $("#materialName").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});

});
function editAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : [ "600px", "54%" ],
		content : rootPath + '/order/editOrderUI.shtml?id=' + cbox
	});
}
function addAccount() {
	pageii = layer.open({
		title : "新增",
		type : 2,
		area : [ "780px", "66%" ],
		content : rootPath + '/order/openOrderManagementUI.shtml'
	});
}
function delAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/order/deleteOrder.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
function view() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择要查看产品");
		return;
	}
	pageii = layer.open({
		title : "物料需求",
		type : 2,
		area : [ "600px", "66%" ],
		content : rootPath + '/order/openMaterial.shtml?id=' + cbox
	});
}

function exportExcel(excelName){
    // var mytable = "";//需要导出的数据
    var fileName = excelName+".xlsx"

    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {//没选则导出所有数据
        var mytable = document.getElementById("mytable");//获取table
        var sheet = XLSX.utils.table_to_sheet(mytable);
    } else {//第二种：导出指定的行

        //获取表头标题，就是表中第一行，序号不存在
        var valueTd= document.getElementById ("mytable").rows[0];

        //复制节点
        var clone_valueTd = valueTd.cloneNode(true);
        // debugger;

        //根据选中行，动态拼接table
        // var tableData = "<table><tr>" + valueTd.innerHTML + "</tr>";
        var table = document.createElement('table');
        table.id = "table1";
        table.appendChild(clone_valueTd);//使用原有节点会对原有dom造成影响

        //获取所有表中所有checkbox
        var inputs = document.getElementById("mytable").getElementsByTagName("input");
        console.log("inputs.length:"+inputs.length);
        for(var i=0;i<inputs.length;i++) {//获取选中行
            if (inputs[i].type == "checkbox") {
                if (inputs[i].checked) {
                    var checkedRow = inputs[i];
                    var tr = checkedRow.parentNode.parentNode;
                    var clone_tr = tr.cloneNode(true);//复制
                    console.log(tr.cells[0].innerText);
                    if(tr.cells[0].innerText != ""){//序号不为空则拼接，为空说明是表头标题
                        // tableData += "<tr>" + tr.innerHTML + "</tr>";
                        table.appendChild(clone_tr)
                    }
                    // console.log("为空是表头，不拼接")
                    // console.log("tr...:"+tr);
                }
            }
        }

        // tableData += "</table>";
        // mytable = document.createElement(tableData);
        var sheet = XLSX.utils.table_to_sheet(table);
    }

    openDownloadDialog(sheet2blob(sheet), fileName);
}